﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Web;

namespace Chat_WebServer.Controllers
{
    public class YahooFriendsController : ApiController
    {
        private const string URL_YM_CONTACT = "http://developer.messenger.yahooapis.com/v1/contacts";
        private const string CONSUMER_KEY = "dj0yJmk9a3hXZFk2VjhxbDE1JmQ9WVdrOVVtbDFhako2TnpRbWNHbzlNVE0yTURjME5qazJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0yMg--";
        private const string CONSUMER_SECRET = "a21a9f4625f6b4fcc5eca8901ac343352b8d4406";
        private OAuthBase oauth = new OAuthBase();
        public List<MyFriend> GET(string oauth_token, string oauth_token_secret, string sessionID)
        {
            List<MyFriend> friends = new List<MyFriend>();
            oauth_token = oauth_token.Replace("=","%3D");
            string requestURL = URL_YM_CONTACT + "?oauth_consumer_key=" + CONSUMER_KEY
                                + "&oauth_nonce=" + oauth.GenerateNonce()
                                + "&oauth_signature=" + CONSUMER_SECRET + "%26" + oauth_token_secret
                                + "&oauth_signature_method=PLAINTEXT"
                                + "&oauth_timestamp=" + oauth.GenerateTimeStamp()
                                + "&oauth_token=" + oauth_token
                                + "&oauth_version=1.0"
                                + "&sid=" + sessionID
                                + "&fields=%2Bpresence"
                                + "&fields=%2Bgroups";
            HttpWebRequest request = HttpWebRequest.Create(requestURL) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/json; charset=utf-8";
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        String str = reader.ReadToEnd();
                        JObject o = JObject.Parse(str);
                        for (int i = 0; i < (int)o["count"]; i++)
                        {
                            MyFriend friend = new MyFriend();
                            friend.Username = (string)o["contacts"][i]["contact"]["id"];
                            if ((int)o["contacts"][i]["contact"]["presence"]["presenceState"] == -1)
                                friend.IsOnl = 0;
                            else
                                friend.IsOnl = 1;
                            friends.Add(friend);
                        }
                    }
                }
            }
            catch
            {
                return friends;
            }
            return friends;
        }
    }
}
